package Character;

import android.graphics.Canvas;
import GameTools.ydLine;
import GameTools.ydMath;
import GameTools.ydPoint;
import GameTools.ydSprite;




public class BodyPart {

	ydLine limb;
	ydSprite skin;
	
	ydPoint j1, j2;
	
	
	
	double angularVelocity;
	
	public BodyPart(ydPoint p1, double length, int imgID){
		limb=new ydLine(p1, length);
		skin=new ydSprite(imgID);
	}
	
	public BodyPart(ydPoint p1, int imgID){
		skin=new ydSprite(imgID);
		limb=new ydLine(p1, skin.getWidth());
		
		
		
	}
	
	public BodyPart(ydPoint p1, ydPoint p2){
		limb=new ydLine(p1, p2);
	}
	
	public void draw(Canvas canvas){
		limb.drawLine(canvas);
		skin.draw(canvas, limb);
		
		j1.drawPoint(canvas);
		j2.drawPoint(canvas);
		
	}
	
	public void setFirstJoint(ydPoint dP){
		j1=new ydPoint(dP);
	}
	
	public void setSecondJoint(ydPoint dP){
		j2=new ydPoint(dP);
	}
	
	public ydPoint firstJoint(){		
		ydLine l=new ydLine(limb.p1, skin.pivotPoint.x, limb.slope);
		
		return new ydPoint(l.p2);
	}
	
	public ydPoint secondJoint(){		
		ydLine l=new ydLine(limb.p1, limb.length-skin.pivotPoint.x, limb.slope);
		
		return new ydPoint(l.p2);
	}
	
	public ydPoint firstJoint(ydPoint dP){		
	/*	ydLine l=new ydLine(limb.p1,f, limb.slope);
		
		return new ydPoint(l.p2);*/
		return new ydPoint(dP);
	}
	
	public ydPoint secondJoint(float f){		
		ydLine l=new ydLine(limb.p1, f, limb.slope);
		
		return new ydPoint(l.p2);
	}
	
	public void rotate(double rads){
		limb.rotate(j1, rads);
		j2.rotate(j1, (float) rads);

	}
	
	public void rotate(double rads, int mode){
		if(mode==0){
			limb.rotate(j1, rads);
			j2.rotate(j1, (float) rads);
		}
		else if(mode==1){
			limb.rotate(j2, rads);
			j1.rotate(j2, (float) rads);
		}

	}
	
	public static void swap(BodyPart b1, BodyPart b2){
		
		BodyPart tmp=b1;
		b1=b2;
		b2=tmp;
		
	}
	
	public static ydPoint matchJoints(ydPoint p1, ydPoint p2){
		return new ydPoint(p2.x-p1.x, p2.y-p1.y);
	}

	public void deltaMove(ydPoint shift) {
		limb.deltaMove(shift);
		j1.deltaMove(shift);
		j2.deltaMove(shift);
		
	}

	public void rotateTo(double rads) {
		j2.rotate(j1, (float) -ydMath.degreesToRads((float) limb.angle));
		limb.rotateTo(j1, rads);
		j2.rotate(j1,(float) rads);
	}

	public void setScale(float s) {
		skin.setScale(s);
		limb.setScale(skin.getSize());
		
	}

	public void recalculateWithSlope() {
		limb.recalculateWithSlope(skin.getWidth());
		
		
	}

	public void moveTo(ydPoint p) {
		ydPoint shift=new ydPoint(p.x-limb.p1.x, p.y-limb.p1.y);
		limb.moveTo(p);
		j1.deltaMove(shift);
		j2.deltaMove(shift);
		
	}

	
}
